const { response } = require("express");
var express = require("express");
var app = new express();
const http = require("http");
var https = require("https");
var axios = require("axios");
app.use(express.static(__dirname));

app.all("*", (req, res, next) => {
  // google需要配置，否则报错cors error
  res.setHeader("Access-Control-Allow-Credentials", "true");
  // 允许的地址,http://127.0.0.1:9000这样的格式
  res.setHeader("Access-Control-Allow-Origin", req.get("Origin"));
  // 允许跨域请求的方法
  res.setHeader(
    "Access-Control-Allow-Methods",
    "POST, GET, OPTIONS, DELETE, PUT"
  );
  // 允许跨域请求header携带哪些东西
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since"
  );
  next();
});
app.get("/map", async function (req, res) {
  console.log(req.query);
  try {
    let result =await axios.get(
        `https://geo.datav.aliyun.com/areas_v3/bound/${req.query.jsonName}`,
        {}
      );
      console.log(result)
      let data = result.data
      res.send(data)
  } catch (error) {
      console.log('error:'+error)
  }
  
//   if (result.indexOf("xml") == -1) {
//     res.send(JSON.parse(result));
//   } else {
//     res.send(null);
//   }
  // let body = ''
  // https.get(url=`https://geo.datav.aliyun.com/areas_v3/bound/${req.query.jsonName}`,(response)=>{
  //     response.on('data',(data)=>{
  //         body +=data
  //     }).on('end',()=>{
  //         if(body.indexOf('xml')==-1){
  //             res.send(JSON.parse(body))
  //         }else{
  //             res.send(null)
  //         }

  //     })
  // })
});
(async()=>{
    const fs = require('fs')
    const path = require('path')
    let file = path.resolve(__dirname, './file.json')
    let area ={
      "110000": "北京市",
      "120000": "天津市",
      "130000": "河北省",
      "140000": "山西省",
      "150000": "内蒙古自治区",
      "210000": "辽宁省",
      "220000": "吉林省",
      "230000": "黑龙江省",
      "310000": "上海市",
      "320000": "江苏省",
      "330000": "浙江省",
      "340000": "安徽省",
      "350000": "福建省",
      "360000": "江西省",
      "370000": "山东省",
      "410000": "河南省",
      "420000": "湖北省",
      "430000": "湖南省",
      "440000": "广东省",
      "450000": "广西壮族自治区",
      "460000": "海南省",
      "500000": "重庆市",
      "510000": "四川省",
      "520000": "贵州省",
      "530000": "云南省",
      "540000": "西藏自治区",
      "610000": "陕西省",
      "620000": "甘肃省",
      "630000": "青海省",
      "640000": "宁夏回族自治区",
      "650000": "新疆维吾尔自治区",
      "710000": "台湾省",
      "810000": "香港特别行政区",
      "820000": "澳门特别行政区"
    }
    // let arr = []
    // for (const key in area) {
    //   arr.push({
    //     name:area[key],
    //     adcode:key
    //   })
    //   let filepath = path.resolve(__dirname, `adcode.json`)
    // fs.writeFileSync(filepath, JSON.stringify(arr))
    // }
    // https://geo.datav.aliyun.com/areas_v3/bound/all.json    adcode
    let data = fs.readFileSync(path.resolve(__dirname, `file.json`))
    data = JSON.parse(String(data))
    for (const key of data) {
      try {
        let code =await axios.get(
          `https://geo.datav.aliyun.com/areas_v3/bound/${key.adcode}_full.json`,
          {}
        );
        let data = code.data
        let filepath = path.resolve(__dirname, `./chinesedata/${key.name}.json`)
        fs.writeFileSync(filepath, JSON.stringify(data))
      } catch (error) {
        console.log('error:'+error)
      }
      
    }

    // let code =await axios.get(
    //     `https://geo.datav.aliyun.com/areas_v3/bound/all.json`,
    //     {}
    //   );
    //   let data = code.data
    // fs.writeFileSync(file, JSON.stringify(data))
}

)()
var server = app.listen(9999, function () {
  console.log("server is started.");
});
